package com.meiqiongproperty.yiguanjia.fragment.user;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.TenementAddressLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.SimpleHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.GetRegion;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.getHouse;
import com.meiqiongproperty.yiguanjia.httpbean.HouseBean;
import com.meiqiongproperty.yiguanjia.httpbean.TenementAddressBean;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

/**
 * 社区资讯
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.user
 * @User ChenJing
 * @Date 2017/5/10
 */

public class TenementAddress extends BaseFragment {
    private TenementAddressLayoutBinding binding;
    private TenmentAdapter adapter;
    public IOnItemListener onItemListener;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.tenement_address_layout, container, false);
        binding.setClick(this);
        initView();
        return binding.getRoot();
    }

    private void initView() {
        binding.navigation.setTitle("选择小区");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        String flag="E5AFAFAD7310B883019133E6E816DA71";
        GetRegion getRegion=new GetRegion();
        getRegion.setFlag(flag);
        ApiService.getRegion(getActivity(), new SimpleHttpCallback<TenementAddressBean>() {
            @Override
            public void onNext(TenementAddressBean tenementAddressBean) {
                LogUtil.d("tenementAddressBean="+tenementAddressBean.getCity().toString());
                getTenmentList(tenementAddressBean);
            }
            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
//                Utils.ShowMsg(message, getActivity());
            }
        }, Utils.getRequestBody(getRegion));
    }

    public void CancelClick(View view) {

    }

    public void getTenmentList(TenementAddressBean tenementAddressBean) {
        getHouse gethouse=new getHouse();
        gethouse.setCityId(tenementAddressBean.getCity().get(0).getId());
        gethouse.setCityName(tenementAddressBean.getCity().get(0).getCityName());
        ApiService.getHouse(getActivity(), new SimpleHttpCallback<HouseBean>() {
            @Override
            public void onNext(final HouseBean houseBean) {
                LogUtil.d("houseBean="+houseBean.getHouse().size());
                adapter=new TenmentAdapter(getActivity(),houseBean.getHouse());
                LinearLayoutManager manager = new LinearLayoutManager(getActivity());
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                binding.list.setLayoutManager(manager);
                binding.list.setAdapter(adapter);
                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        onItemListener.setListData(houseBean.getHouse().get(position).getHousesName(),houseBean.getHouse().get(position).getId());
                        sendKeyBackEvent();
                    }
                });
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },Utils.getRequestBody(gethouse));
    }

    public void setData(IOnItemListener onItemListener){
        this.onItemListener=onItemListener;
    }

    public interface IOnItemListener{
        public void setListData(String address,String houseId);
    }
}
